import {Component, OnInit} from '@angular/core';
import {CommonService} from '../common.service';

@Component({
    selector: 'app-list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit {
    list = {
        total: '',
        per_page: '',
        current_page: '',
        last_page: '',
        data: []
    };
    channel = this.Service.getChannel();
    loading = true;
    tags = [];
    actors = [];
    params;
    constructor(public Service: CommonService) {
    }

    ngOnInit() {
        this.getList();
        this.getTags();
        if (this.channel === 'video') {
            this.getActors();
        }


        console.log($('.hy-cascade .item .tags li.active').offsetLeft)
    }

    getList() {
        const $this = this;
        const params = this.Service.getUrlParams();
        params['channel'] = this.channel;
        params['actor'] = params['actor'] ? decodeURI(params['actor']) : '';
        params['tag'] = params['tag'] ? decodeURI(params['tag']) : '';
        params['row'] = 18;
        this.params = params;
        this.Service.post('index/getlist', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.list = data;
                if(!data.total){
                    $this.loading = false;
                }
            });
        });
    }


    /**
     * 获取url
     * @param key
     * @param value
     */
    getPageUrl(key = '', value = '') {
        const params = this.Service.getUrlParams();
        const url = this.Service.getUrlPath();
        let urlParams = '';
        if (key) {
            params[key] = value;
        }

        for (const i in params) {
            urlParams += i + '=' + params[i] + '&';
        }

        urlParams = urlParams.substring(0, urlParams.lastIndexOf('&'));
        return '/' + url + '?' + urlParams;
    }

    /**
     * 获取标签
     */
    getTags() {
        const $this = this;
        const params = {};
        params['channel'] = this.channel === 'clips' ? 'video' : this.channel;
        params['row'] = 999;

        this.Service.post('index/gettags', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.tags = data;
            });
        });
    }

    /**
     * 获取演员列表
     */
    getActors() {
        const $this = this;
        const params = {};
        params['row'] = 999;

        this.Service.post('index/getactors', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.actors = data;
            });
        });
    }
}
